package cn.gailvlun.gll.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import cn.gailvlun.gll.net.R;


public class FilterView extends FrameLayout {

  public enum Type {
    Filter, Menu
  }

  
  private final int maskColor;
  
  private boolean clickMaskToClose;
  
  private int filterOpenAnim;
  
  private int filterCloseAnim;
  
  private int menuOpenAnim;
  
  private int menuCloseAnim;

  
  private FrameLayout contentView;
  
  private View maskView;

  private View filterContentView;

  private View menuContentView;

  
  private boolean filterShow;
  
  private boolean menuShow;

  
  

  public FilterView(@NonNull Context context) {
    this(context, null);
  }

  public FilterView(@NonNull Context context, @Nullable AttributeSet attrs) {
    
    this(context, attrs, R.attr.FilterViewStyle);
  }

  public FilterView(@NonNull Context context, @Nullable AttributeSet attrs,
      @AttrRes int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FilterView, defStyleAttr, 0);
    maskColor = a.getColor(R.styleable.FilterView_mask_color, 0x88888888);
    clickMaskToClose = a.getBoolean(R.styleable.FilterView_click_mask_to_close, true);
    filterOpenAnim = a.getResourceId(R.styleable.FilterView_filter_open_anim, R.anim.drop_down);
    filterCloseAnim = a.getResourceId(R.styleable.FilterView_filter_close_anim, R.anim.drop_up);
    menuOpenAnim = a.getResourceId(R.styleable.FilterView_menu_open_anim, R.anim.drop_down);
    menuCloseAnim = a.getResourceId(R.styleable.FilterView_menu_close_anim, R.anim.drop_up);
    filterShow = a.getBoolean(R.styleable.FilterView_filter_show, false);
    menuShow = a.getBoolean(R.styleable.FilterView_menu_show, false);
    a.recycle();
  }

  
  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    init();
  }

  private void init() {
    contentView = new FrameLayout(getContext());
    ViewGroup.LayoutParams contentLp = new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    contentView.setLayoutParams(contentLp);
    contentView.setVisibility(GONE);
    
    
    
    
    if (isInEditMode()) {
      if (filterShow || menuShow) {
        contentView.setVisibility(VISIBLE);
      }
    }
    
    contentView.setId(R.id.filter_content_view);

    filterContentView = findViewById(R.id.filter);
    menuContentView = findViewById(R.id.menu);

    removeAllViews();

    if (filterContentView != null) {
      filterContentView.setVisibility(GONE);
      
      if (isInEditMode()) {
        if (filterShow) {
          filterContentView.setVisibility(VISIBLE);
        }
      }
      filterContentView.setClickable(true);
      contentView.addView(filterContentView);
    }
    if (menuContentView != null) {
      menuContentView.setVisibility(GONE);
      
      if (isInEditMode()) {
        if (menuShow) {
          menuContentView.setVisibility(VISIBLE);
        }
      }
      menuContentView.setClickable(true);
      contentView.addView(menuContentView);
    }

    
    maskView = new View(getContext());
    maskView.setBackgroundColor(maskColor);
    ViewGroup.LayoutParams maskLp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
    maskView.setLayoutParams(maskLp);
    maskView.setVisibility(GONE);
    setClickMaskToClose(clickMaskToClose);
    maskView.setId(R.id.filter_mask_view);
    addView(maskView, 0);

    addView(contentView, 1);
  }

  
  public void open(Type type) {
    if (type == Type.Filter) {
      if (filterContentView == null) {
        return;
      }
      if (isOpen(Type.Menu)) {
        close();
        return;
      }
      isFilterOpening = true;
      filterContentView.setVisibility(VISIBLE);
      if (menuContentView != null) {
        menuContentView.setVisibility(GONE);
      }
    } else if (type == Type.Menu) {
      if (menuContentView == null) {
        return;
      }
      if (isOpen(Type.Filter)) {
        close();
        return;
      }
      isMenuOpening = true;
      menuContentView.setVisibility(VISIBLE);
      if (filterContentView != null) {
        filterContentView.setVisibility(GONE);
      }
    }
    
    maskView.setVisibility(VISIBLE);
    maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.mask_in));

    
    contentView.setVisibility(VISIBLE);
    contentView.setAnimation(AnimationUtils.loadAnimation(getContext(),
        type == Type.Filter ? filterOpenAnim : menuOpenAnim));

    if (mOnToggleListener != null) {
      mOnToggleListener.onToggle(true, type);
    }
  }

  public void open() {
    open(Type.Filter);
  }

  
  public void close() {
    
    contentView.setVisibility(GONE);
    Type openType = getOpenType();
    contentView.setAnimation(AnimationUtils.loadAnimation(getContext(),
        openType == Type.Filter ? filterCloseAnim : menuCloseAnim));

    
    maskView.setVisibility(GONE);
    maskView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.mask_out));

    isFilterOpening = false;
    isMenuOpening = false;

    if (mOnToggleListener != null) {
      mOnToggleListener.onToggle(false, openType);
    }
  }

  
  public void toggle() {
    if (isOpen()) {
      close();
    } else {
      open();
    }
  }

  
  public void toggle(Type type) {
    if (isOpen()) {
      close();
    } else {
      open(type);
    }
  }

  private boolean isFilterOpening = false;
  private boolean isMenuOpening = false;

  
  public boolean isOpen(Type type) {
    switch (type) {
    case Filter: {
      return isFilterOpening;
    }
    case Menu: {
      return isMenuOpening;
    }
    default: {
      return isOpen();
    }
    }
  }

  public Type getOpenType() {
    return isOpen(Type.Filter) ? Type.Filter : Type.Menu;
  }

  
  public boolean isOpen() {
    return isFilterOpening || isMenuOpening;
  }

  
  public void setClickMaskToClose(boolean closeOrNot) {
    clickMaskToClose = closeOrNot;
    maskView.setOnClickListener(closeOrNot ? mOnClickListener : null);
  }

  
  private OnClickListener mOnClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
      if (isOpen()) {
        close();
      }
    }
  };

  
  public void setFilterOpenAnim(int filterOpenAnim) {
    this.filterOpenAnim = filterOpenAnim;
  }

  
  public void setFilterCloseAnim(int filterCloseAnim) {
    this.filterCloseAnim = filterCloseAnim;
  }

  
  @Override
  public void addView(View child) {
    judgeChild(child);
    super.addView(child);
  }

  @Override
  public void addView(View child, int index) {
    judgeChild(child);
    super.addView(child, index);
  }

  @Override
  public void addView(View child, int index, ViewGroup.LayoutParams params) {
    judgeChild(child);
    super.addView(child, index, params);
  }

  @Override
  public void addView(View child, ViewGroup.LayoutParams params) {
    judgeChild(child);
    super.addView(child, params);
  }

  @Override
  public void addView(View child, int width, int height) {
    judgeChild(child);
    super.addView(child, width, height);
  }

  
  private void judgeChild(View child) {
    
    int id = child.getId();
    if (id != R.id.filter && id != R.id.menu && id != R.id.filter_content_view
        && id != R.id.filter_mask_view) {
      throw new IllegalStateException("id 必须为 filter 或者 menu");
    }
  }

  public interface OnToggleListener {
    void onToggle(boolean isOpen, Type type);
  }

  private OnToggleListener mOnToggleListener;

  public void setOnToggleListener(OnToggleListener onToggleListener) {
    if (onToggleListener != null) {
      mOnToggleListener = onToggleListener;
    }
  }
}